<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {
            var params = getParams();

            var num ;//总数
            var state;
            //收藏数量
            $.get("/news/nums",{newsId:params.id,type:2},function (data) {
                num = data;
                $("#collectBtn").html(num)
            })

            var num1 ;//总数
            var state1;
            //点赞数量
            $.get("/news/praiseNum",{newsId:params.id,type:2},function (data) {
                num1 = data;
                $("#likeBtn").html(num1)
            })

            if (params.id) {
                //获取作用域中的用户信息
                var user = JSON.parse(sessionStorage.getItem("user"));

                if(user){
                    //查询当前用户的收藏情况
                    $.get("/strategy/collectionCount", {travelId: params.id, userId: user.id,type:2}, function (data) {
                        if (data) {
                            $("#collectBtn").css("color", "orange").removeClass("fa-star-o").addClass("fa-star")
                        } else {
                            $("#collectBtn").css("color", "black").removeClass("fa-star").addClass("fa-star-o")
                        }
                    })
                }

                $("#collectBtn").click(function () {
                    if (user!=null) {
                        $.get("/strategy/collectionCount", {travelId: params.id, userId: user.id,type:2}, function (data) {
                            if (data) {
                                $.get("/strategy/" + params.id, function (data) {
                                    $.ajax({
                                        type: "delete",
                                        url: "/strategy/" +params.id,
                                        success: function () {
                                            $("#collectBtn").css("color", "black").removeClass("fa-star").addClass("fa-star-o")
                                        }
                                    });
                                });
                                state = false;
                            } else {
                                $.post("/strategy/collection", {travelId: params.id, userId: user.id,type:2}, function (data) {
                                    if (data.success) {
                                        $("#collectBtn").css("color", "orange").removeClass("fa-star-o").addClass("fa-star")
                                    }
                                })
                                state = true;
                            }
                            if (state) {
                                num++;
                            }else{
                                num--;
                            }
                            $("#collectBtn").html(num);
                        })

                    }else {
                        //没有登录,跳转到登录页面
                        $(document).dialog({
                            autoClose: 1200,
                            content: '1.5秒后跳转到登录页面'
                        });

                        setTimeout(function () {
                            window.location.href = "/login.html"
                        },1500);

                    }
                })

                //点赞
                if (user) {
                    //用户登录且有点赞过显示红色
                    $.get("/praises", {travelId: params.id, userId: user.id,type:2}, function (data) {
                        if (data) {
                            $("#likeBtn").css("color", "orange").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                        }
                    })
                } else {
                    //没有登录时该图标为黑色
                    $("#likeBtn").css("color", "").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                }

                //点赞或取消点赞
                $("#likeBtn").click(function () {
                    if (user != null) {
                        //判断该用户是否有点赞
                        $.get("/praises", {travelId: params.id, userId: user.id,type:2}, function (data) {
                            if (data) {
                                $.ajax({
                                    type: "delete",
                                    url: "/praises/" + params.id+"/strategy",
                                    success: function () {
                                        $("#likeBtn").css("color", "").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up")
                                    }
                                });
                                state1 = false
                            }else {
                                $.post("/praises", {travelId: params.id, userId: user.id,type:2}, function (data) {
                                    if (data.success) {
                                        $("#likeBtn").css("color", "orange").removeClass("fa-thumbs-o-up").addClass("fa-thumbs-up")
                                    }
                                })
                                state1 = true;
                            }
                            if (state1) {
                                num1++;
                            }else{
                                num1--;
                            }
                            $("#likeBtn").html(num1);
                        })

                    } else {
                        //没有登录,跳转到登录页面
                        $(document).dialog({
                            autoClose: 1200,
                            content: '1.5秒后跳转到登录页面'
                        });

                        setTimeout(function () {
                            window.location.href = "/login.html"
                        },1500);
                    }
                })

            }

            //根据大攻略id查出热门标签
            $.get('/strategy/'+params.id+"/tags",function (data) {
                var tags={list:data}
                $(".tag").renderValues(tags)
            })


            //根据大攻略id查出大攻略
            $.get("/strategy/" + params.id, function (data) {
                $(".strategies").renderValues(data);
            })

            //根据大攻略id查出改攻略的detail和content
            $.get("/strategy/" + params.id + "/strategycatalog", function (data) {
                $(".catalogs").renderValues(data,{
                    getHref:function (item,value) {
                        var url=$(item).data("href");
                        $(item).attr('href',url+value);
                    }
                });
            })

            //分页
            var currentPage =1;
            var travelsDate=[];
            var pages;
            var flag= false;//表示当前没有在查询
            $(function () {
                function loadTravels() {
                    if (flag) {
                        return;

                    }
                    flag = true;
                    //根据大攻略id查出该攻略的点评
                    $.get("/strategy/" + params.id + "/comments", {currentPage: currentPage}, function (data) {
                        console.log(data);
                        $.merge(travelsDate, data.list);
                        var ret = {"list": travelsDate}
                        $("#comment").renderValues(ret, {
                            getHref: function (item, value) {
                                var href = $(item).data("href")
                                $(item).prop("href", href + value);
                            },
                            getStar:function (item,value) {
                                var temp="";
                                for(var i=0;i<value;i++){
                                    temp+='<i class="fa fa-star"></i>';
                                }
                                for(var i=0;i<5-value;i++){
                                    temp+=' <i class="fa fa-star-o"></i>';
                                }
                                $(item).html(temp);
                            }
                        });


                        //给图片绑定点击事件
                        $(".comment-img li img").click(function () {
                            var img=$(this).attr("src");
                            $(document).dialog({
                                overlayClose: true,
                                titleShow: false,
                                content: '<img class="info-icon" width="100%" src="'+img+'"/>'
                            });

                        })
                        currentPage++
                        pages = data.pages;
                        flag = false;//表示查询完成
                    });
                }

                loadTravels();

                $(window).scroll(function () {
                    if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                        if (pages >= currentPage) {
                            loadTravels();
                        } else {
                            $(document).dialog({
                                type: 'notice',
                                infoText: '亲,到底了',
                                autoClose: 2500,
                                position: 'bottom'
                            });
                        }
                    }
                });

            })

            $("#commentBtn").click(function () {
                location.href="/mine/addComment.html?id="+params.id;
            })



        })
    </script>

</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title" render-html="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-star-o" id="collectBtn"> <span> </span> 收藏</i>
        <i class="fa fa-thumbs-o-up"  id="likeBtn"> <span> </span>点赞</i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div>
                <a href="travelContent.html">
                    <button class="btn">上下九</button>
                </a><a href="travelContent.html">
                <button class="btn">入住长隆熊猫酒店，天天都是儿童节</button>
            </a><a href="travelContent.html">
                <button class="btn">飞到羊城，只为一场最地道的早茶之旅</button>
            </a></div>
        </div>

        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-html="list.name"></h6>
                    <div render-loop="list.strategyDetail">
                        <a data-href="strategyDetails.html?id=" render-key="list.strategyDetail.id" render-fun="getHref">
                        <button class="btn" render-html="list.strategyDetail.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div render-loop="list">
                <span render-html="list.name"></span>

            </div>
        </div>
        <hr>
        <!--点评-->
        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-href="userProfiles.html?id=" render-key="list.user.id" render-fun="getHref">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">
                        </span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div class="comment-content">
                            <a data-href="/mine/strategyComment.html?id=" render-key="list.id" render-fun="getHref">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgArr">
                            <li>
                                <img render-src="list.imgArr.self">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a data-href="/mine/strategyComment.html?id=" render-key="list.id" render-fun="getHref">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                            </a>
                                <i class="fa fa-thumbs-o-up"> <span>0</span> </i>
                        </div>

                    </div>

                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>